<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      title="查看账号"
      :close-on-click-modal="false"
      width="960px"
    >
      <div>
        <div class="search-inner fx-r fx-bc">
          <div class="label">姓名</div>
          <div style="width: 150px" class="value">
            <el-input
              v-model="formValidate.name"
              placeholder="请输入客户姓名"
            />
          </div>
          <div class="label">手机号码</div>
          <div style="width: 150px" class="value">
            <el-input
              v-model="formValidate.mobile"
              placeholder="请输入手机号码"
            />
          </div>
          <div class="value">
            <el-button @click="tapSearch" type="primary" icon="el-icon-zoom-in"
              >搜索</el-button
            >
          </div>
          <div class="fx-g1"></div>
        </div>

        <div>
          <div class="value">
            <el-button @click="tapAdd" type="success" icon="el-icon-plus"
              >添加新账号</el-button
            >
          </div>
        </div>

        <el-table
          :data="listData"
          :max-height="500"
          v-loading="loading"
          style="margin-top: 10px"
          border
          row-key="id"
        >
          <el-table-column label="序" align="center" prop="name" width="80px">
            <template #default="scope">
              {{ scope.$index + 1 + page.pageSize * (page.page - 1) }}
            </template>
          </el-table-column>

          <el-table-column
            label="姓名"
            align="center"
            prop="name"
            width="120px"
          >
          </el-table-column>
          <el-table-column
            label="手机号"
            align="center"
            prop="mobile"
            width="120px"
          >
          </el-table-column>

          <el-table-column label="职位" align="center" prop="job" width="120px">
          </el-table-column>

          <el-table-column label="性别" align="center" prop="sex" width="80px">
            <template #default="scope">
              <div v-if="scope.row.sex == 1">男</div>
              <div v-if="scope.row.sex == 2">女</div>
              <div v-if="scope.row.sex == 0">未知</div>
            </template>
          </el-table-column>

          <el-table-column
            label="单位信息"
            align="center"
            prop="bumen_name"
            width="120px"
          />

          <el-table-column label="状态" align="center" width="100px">
            <template #default="scope">
              <div
                v-if="scope.row.status == 1"
                style="font-weight: bold; font-size: 14px"
              >
                正常
              </div>
              <div
                v-if="scope.row.status == -1"
                style="color: #ce3c39; font-weight: bold; font-size: 14px"
              >
                禁用
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="140"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <a style="color: #cf9236" @click="tapEdit(scope.row.id)"
                >编辑数据</a
              >
              <Divider type="vertical" />
              <a style="color: #bd2c00" @click="tapDel(scope.row.id)"
                >删除数据</a
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="acea-row row-right page">
          <Page
            :total="page.pageCount"
            show-elevator
            show-total
            @on-change="tapPage"
            :page-size="page.pageSize"
          />
        </div>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogVisible2"
      title="账号操作"
      :close-on-click-modal="false"
      width="620"
    >
      <el-form :model="form" ref="form" label-width="120px">
        <el-row :space="24">
          <el-col :span="12">
            <el-form-item
              label="姓名"
              prop="name"
              :rules="[{ required: true, message: '请输入姓名' }]"
            >
              <el-input
                type="text"
                style="width: 240px"
                placeholder="姓名"
                v-model="form.name"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              label="手机号码"
              prop="mobile"
              :rules="[{ required: true, message: '请输入手机号码' }]"
            >
              <el-input
                type="text"
                style="width: 240px"
                placeholder="手机号码	"
                v-model="form.mobile"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="12">
            <el-form-item label="性　别">
              <el-radio-group v-model="form.sex" class="ml-4">
                <el-radio :label="1" size="large">男</el-radio>
                <el-radio :label="2" size="large">女</el-radio>
                <el-radio :label="0" size="large">未知</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="联系地址">
              <el-input
                type="text"
                style="width: 240px"
                placeholder="联系地址"
                v-model="form.address"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="12">
            <el-form-item label="登录密码">
              <el-input
                type="text"
                style="width: 240px"
                placeholder="请输入登录密码"
                v-model="form.password"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="职位">
              <el-input
                type="text"
                style="width: 240px"
                placeholder="请输入职位"
                v-model="form.job"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                :rows="2"
                type="textarea"
                v-model="form.mono"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="24">
            <el-form-item label="显示:" style="margin-bottom: 0">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer fx-r fx-ae">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="subAdmin">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  BumenUserSave,
  SysteBumenDel,
  SysteBumenUserDel,
  SystemBumenUserInfo,
  SystemBumenUserList,
} from '../../../../api/system/bumen'

export default {
  name: 'bumen_info_member',
  data() {
    return {
      dialogVisible: false,
      dialogVisible2: false,
      listData: [],
      loading: false,
      formValidate: {},
      bumenId: 0,
      page: {
        pageCount: 0, // 一共多少数量
        pageSize: 0, // 每一页码数量
        page: 1, // 当前页码
      },
      form: {
        level_id: 1,
      },
    }
  },
  //创建页面
  created() {},
  //页面初始化成功
  mounted() {},
  //页面销毁
  unmounted() {},

  methods: {
    open: function (bumenId) {
      this.bumenId = bumenId
      this.dialogVisible = true
      this.getData()
    },
    /**
     * 添加数据
     * @param bumen_id
     */
    tapAdd: function () {
      this.form = { sex: 0, id: 0, bumen_id: this.bumenId, status: 1 }
      this.dialogVisible2 = true
    },

    tapSearch: function () {
      this.page.page = 1
      this.getData()
    },

    tapPage: function (page) {
      this.page.page = page
      this.getData()
    },

    /**
     * 列表数据
     */
    getData: function () {
      this.loading = true
      SystemBumenUserList({
        ...this.formValidate,
        page: this.page.page,
        bumen_id: this.bumenId,
      })
        .then((res) => {
          this.$utils.loadingClose()
          this.loading = false
          if (res.code == 200) {
            this.listData = res.data.data
            this.page.pageSize = res.data.pageSize
            this.page.pageCount = res.data.pageCount
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch((err) => {
          this.$utils.loadingClose()
          this.$message.error('网络繁忙，加载失败，请稍等片刻在尝试!')
        })
    },

    /**
     * 修改管理员
     */
    tapEdit: function (uid) {
      this.$utils.loading(this, '获取数据中...')
      SystemBumenUserInfo({ id: uid })
        .then((res) => {
          this.$utils.loadingClose()
          if (res.code == 200) {
            this.dialogVisible2 = true
            this.title = '修改账号'
            this.form = res.data
          } else {
            this.$alert(res.msg)
          }
        })
        .catch((err) => {
          this.$utils.loadingClose()
          this.$alert('网络繁忙，加载失败，请稍等片刻在尝试!', '系统提示')
        })
    },

    /**
     * 提交数据
     */
    subAdmin: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$utils.loading(this, '提交数据中...')
          BumenUserSave(this.form)
            .then((res) => {
              this.$utils.loadingClose()
              if (res.code == 200) {
                this.$message({ message: res.msg, type: 'success' })
                this.dialogVisible2 = false
                this.page.page = 1
                this.getData()
                //getUid
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch((err) => {
              this.$utils.loadingClose()
              this.$message.error('网络繁忙，加载失败，请稍等片刻在尝试!')
            })
        }
      })
    },

    tapDel(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$utils.loading(this, '删除数据中...')
        SysteBumenUserDel({ id: id })
          .then((res) => {
            this.$utils.loadingClose()
            if (res.code == 200) {
              this.getData()
            } else {
              this.$alert(res.msg)
            }
          })
          .catch((err) => {
            this.$utils.loadingClose()
            this.$alert('网络繁忙，加载失败，请稍等片刻在尝试!', '系统提示')
          })
      })
    },
  },
}
</script>

<style scoped></style>
